package com.qckj.amz.utils.amzApi;

import com.amazon.SellingPartnerAPIAA.*;
import io.swagger.client.ApiException;
import io.swagger.client.api.FeedsApi;
import io.swagger.client.api.OrdersV0Api;
import io.swagger.client.api.SellersApi;
import lombok.Data;

import java.util.UUID;

/**
 *
 * 创建sp-api客户端
 *
 * 当前类仅做spapi创建客户端使用，请勿添加无相关的代码
 *
 * 当前采用java构造方法创建，在使用new AmzSpApiUtils()时即可获取到客户端。
 *
 * 若构造方法同时创建多个客户端影响效率或其他问题，可通过返回对应的客户端创建各自的方法进行获取。
 *
 * @author luchuan
 * @version v1.0
 * @date 2022/12/2 0002
 **/
@Data
public class AmzSpApiUtils {

    private SellersApi sellersApi;
    private FeedsApi feedsApi;
    private OrdersV0Api ordersV0Api;

    public AmzSpApiUtils(String aws_region, String lwa_endpoint_url, String refresh_token, String selling_endpoint_url) throws ApiException {

        /*
         * 用户 IAM ARN
         * */
        AWSAuthenticationCredentialsProvider awsAuthenticationCredentialsProvider = AWSAuthenticationCredentialsProvider.builder()
                .roleArn(AmzSpApiConfig.IAM_ROLE_ARN)
                .roleSessionName(UUID.randomUUID().toString().replace("-", ""))
                .build();
        /*
         * APP应用
         * */
        LWAAuthorizationCredentials lwaAuthorizationCredentials = LWAAuthorizationCredentials.builder()
                .clientId(AmzSpApiConfig.APP_CLIENT_ID)
                .clientSecret(AmzSpApiConfig.APP_CLIENT_SECRET)
                .refreshToken(refresh_token)
                .endpoint(lwa_endpoint_url)
                .build();

        /**
         * 频率限制-若不同api.Builder()构造频率需求不同时，可单独创建
         * RateLimitConfiguration
         */
        RateLimitConfiguration limitOption = RateLimitConfigurationOnRequests.builder()
                .rateLimitPermit(1.0)
                .waitTimeOutInMilliSeconds(2000L)
                .build();

        /**
         * 卖家SellersApi
         */
        SellersApi sellersApi = new SellersApi.Builder()
                .awsAuthenticationCredentials(awsAuthenticationCredentials)
                .lwaAuthorizationCredentials(lwaAuthorizationCredentials)
                .awsAuthenticationCredentialsProvider(awsAuthenticationCredentialsProvider)
                .endpoint(selling_endpoint_url)
                .rateLimitConfigurationOnRequests(limitOption)
                .build();

        /**
         * 产品FeedsApi
         */
        FeedsApi feedsApi = new FeedsApi.Builder()
                .awsAuthenticationCredentials(awsAuthenticationCredentials)
                .lwaAuthorizationCredentials(lwaAuthorizationCredentials)
                .awsAuthenticationCredentialsProvider(awsAuthenticationCredentialsProvider)
                .endpoint(selling_endpoint_url)
                .rateLimitConfigurationOnRequests(limitOption)
                .build();

        /**
         * 订单OrdersV0Api
         */
        OrdersV0Api ordersV0Api = new OrdersV0Api.Builder()
                .awsAuthenticationCredentials(awsAuthenticationCredentials)
                .lwaAuthorizationCredentials(lwaAuthorizationCredentials)
                .awsAuthenticationCredentialsProvider(awsAuthenticationCredentialsProvider)
                .endpoint(selling_endpoint_url)
                .rateLimitConfigurationOnRequests(limitOption)
                .build();

        this.sellersApi = sellersApi;
        this.feedsApi = feedsApi;
        this.ordersV0Api = ordersV0Api;
    }

}
